// pages/order/groupShare/index.js
var app = getApp(), fetchApi = require("../../../utils/httpPost.js"), common_Js = require("../../../component/common.js")
import Toast from '../../../component/vant/toast/toast';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    time: 0,// 30 * 60 * 60 * 1000,
    timeData: {},
    group_id:0,//团购id
    order_id:0,//订单id
    pinkId:0,//成团id
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    if(options.order_id){
      this.data.order_id = parseInt(options.order_id);
    }
    this.onHttpInit();
    this.initWebsocket();
  },
  onUnload: function () {
  	//返回操作
    console.log('onUnload');
    //注意这里有时序问题，
    //如果 wx.connectSocket 还没回调 wx.onSocketOpen，而先调用 wx.closeSocket，那么就做不到关闭 WebSocket 的目的。
    //必须在 WebSocket 打开期间调用 wx.closeSocket 才能关闭。
    wx.closeSocket()
    wx.onSocketClose(function(res) {
      console.log('WebSocket 已关闭！')
    })
  },
  initWebsocket:function(){
    var that = this;
    console.log('initWebsocket',this);
    console.log('initWebsocket url ',fetchApi.getWebSokectUrl()  + "/" + that.data.order_id);
    
   var receivetemp = this.data.receive;
    //建立连接
    wx.connectSocket({
      url: fetchApi.getWebSokectUrl(),
    })

    //连接成功
    wx.onSocketOpen(function () {
      wx.sendSocketMessage({
        data: 'connect|group_' + that.data.order_id,
      })
      console.log("连接成功");
    })

    //接收数据
    wx.onSocketMessage(function (data) {
      console.log('客户端回复的消息',data);
      if(data.data!='connect'){
        var retdat = JSON.parse(data.data);
        console.log('retdat',retdat)
        if(retdat.code == 0){
          that.onHttpInit();
        }else{
          console.log('失败',data);
        }
      }
    })

    //连接失败
    wx.onSocketError(function () {
      console.log('websocket连接失败！');
    })
  },
  onChange(e) {
    this.setData({
      timeData: e.detail,
    });
  },
  finished(e){

  },
  /**
     * 获取页面数据的 http请求
     */
  onHttpInit: function () {
    var that = this
    fetchApi.httpPost('/product/order/group/detail', {
      order_id: that.data.order_id,
    }, function (res) {
      console.log("订单详情数据", res)
      that.setData({ isLoading: false,   loading: true, netWork: true  })
      if (res.data.code == 0) {
        var order = res.data.data.order;
        console.log('order', order);
        that.data.group_id = order.groupId;
        that.data.pinkId = order.pinkId;
        that.data.time = order.groupExpireTime;
        that.setData({
          time: that.data.time,
          order: order, 
          address: res.data.data.address
        })
      }
    }, function (res) {
        that.setData({ hasData: true, isLoading: false, loading: true,  netWork: true })
    })
  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    var that = this;
    var group_id = this.data.group_id;
    var pinkId = this.data.pinkId;
    var user_id = app.getStorageSync("user_id");
    console.log( "转发成功 user_id",user_id)
    return {
      title: "差你1个就成团啦~",
      path: '/pages/product/group/index?id='+group_id+'&pink_id='+pinkId+'&ag_id='+user_id,
      success: function (res) {
        console.log(res, "转发成功")
      },
      fail: function (res) {
        console.log(res, "转发失败")
      }
    }
  },
  goOrderList(e){
    wx.redirectTo({
      url: '/pages/order/productList/index?from_type=0&state=-1&index=0',
    })
  }
})